package com.ctn.information.view;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListAdapter;

import com.ctn.information.R;
import com.ctn.information.util.AppUtil;
import com.ctn.information.view.xlistview.XListView;
import com.ctn.information.view.xlistview.XListView.IXListViewListener;

/**
 * 中心区域面板 - 上拉刷新下拉更多示例
 * 
 * @author HLJ
 *
 */
public abstract class XListViewFragment extends Fragment implements IXListViewListener, View.OnTouchListener{
	
	/**
	 * 上拉刷新下拉更多主体
	 */
	protected com.ctn.information.view.xlistview.XListView mListView;
	
	/**
	 * 上拉刷新下拉更多数据适配器
	 */
	protected ListAdapter mListAdapter;
	
	/**
	 * 更新线程
	 */
	private Handler mHandler;
	
	/**
	 * 是否能够上拉刷新下拉更多
	 */
	private boolean pullLoadEnable = true;
	
	/**
	 * 要加载的Layout ID
	 */
	private int layoutId = R.layout.xlistview;
	
	/**
	 * 要加载的View ID
	 */
	private int xId = R.id.xListView;
	
	/**
	 * 刷新时的提示文本
	 */
	protected String refreshText = "刚刚";
	
	/**
	 * 刷新时间
	 */
	protected int refreshTime = 2000;
	
	/**
	 * 加载更多时间
	 */
	protected int loadTime = 2000;
	
	/**
	 * 左右滑动坐标标记
	 */
	private static float lastValue = -1F;
	
	/**
	 * 是否打开Sliding Menu触摸事件
	 */
	protected boolean openSlidingMenuTouch = false;
	
	/**
	 * Item 可见位置索引
	 */
	protected int pos = 0;
	
	/**
	 * Item top坐标
	 */
	protected int top = 0;
	
	/**
	 * 构造函数
	 */
	public XListViewFragment() { 
		
	}
	
	/**
	 * 设置OpenSlidingMenuTouch
	 * @param flag
	 */
	public void setOpenSlidingMenuTouch(boolean flag){
		openSlidingMenuTouch = flag;
	}
	
	/**
	 * 设置RefreshTime
	 * @param time
	 */
	public void setRefreshTime(int time){
		refreshTime = time;
	}
	
	/**
	 * 设置LoadTime
	 * @param time
	 */
	public void setLoadTime (int time){
		loadTime = time;
	}
	
	/**
	 * 设置刷新文本
	 * @param text
	 */
	public void setRefreshText(String text){
		refreshText = text;
	}
	
	/**
	 * 设置Layout
	 * @param layoutId
	 */
	public void setLayout(int layoutId){
		this.layoutId = layoutId;
	}
	
	/**
	 * 设置XListView
	 * @param xId
	 */
	public void setXListViewId(int xId){
		this.xId = xId;
	}
	
	/**
	 * 设置是否可以上下拉加载数据
	 * @param flag
	 */
	public void setPullLoadEnable(boolean flag){
		pullLoadEnable = flag;
	}
	
	/**
	 * 生成数据
	 * @return
	 */
	public abstract ListAdapter geneListAdapter();

	/**
	 * 刷新数据
	 * @param listAdapter
	 * @return
	 */
	public abstract ListAdapter refresh(ListAdapter listAdapter);
	
	/**
	 * 加载更多旧数据 
	 * @param listAdapter
	 * @return
	 */
	public abstract ListAdapter load(ListAdapter listAdapter);
	
	/**
	 * 创建视图
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(layoutId, null);
		mListAdapter = geneListAdapter();
		mListView = (XListView)v.findViewById(xId);
		mListView.setPullLoadEnable(pullLoadEnable);
		mListView.setAdapter(mListAdapter);
		mListView.setXListViewListener(this);
		mListView.setOnTouchListener(this);
		mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				pos = mListView.getFirstVisiblePosition();
				View v = mListView.getChildAt(0);
				top = (v == null)? 0 : v.getTop();
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
		/** 保存ListView当前位置 */
		mHandler = new Handler();
		return v;
	}
	
	/**
	 * Sliding Menu 绑定触摸事件
	 * 
	 * @param v
	 * @param event
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (openSlidingMenuTouch) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				lastValue = event.getX();
				break;
			case MotionEvent.ACTION_MOVE:
				if (lastValue == -1F) {
					lastValue = event.getX();
				}
				float diff_l = event.getX() - lastValue;
				float diff_r = lastValue - event.getX();
				if (diff_l > 0F && diff_l > AppUtil.DRAG_DIFF_DISTANCE) {
					AppUtil.SLIDING_MENU.showMenu();
				}else if(diff_r > 0F && diff_r > AppUtil.DRAG_DIFF_DISTANCE){
					AppUtil.SLIDING_MENU.showSecondaryMenu();
				}
				break;
			case MotionEvent.ACTION_UP:
				lastValue = -1F;
				break;
			default:
				break;
			}
			return true;
		}else{
			return xListViewTouch(v, event);
		}
	}
	
	/**
	 * XListView触摸事件
	 * @param v
	 * @param event
	 * @return
	 */
	public boolean  xListViewTouch(View v, MotionEvent event){
		return false;
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	/**
	 * 刷新事件
	 */
	@Override
	public void onRefresh() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mListAdapter = refresh(mListAdapter);
				mListView.setAdapter(mListAdapter);
				mListView.stopRefresh();
				mListView.stopLoadMore();
				mListView.setRefreshTime(refreshText);
			}
		}, refreshTime);
	}

	/**
	 * 加载更多旧数据事件
	 */
	@Override
	public void onLoadMore() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				mListAdapter = load(mListAdapter);
				mListView.setAdapter(mListAdapter);
				mListView.setSelectionFromTop(pos, top);
				mListView.stopRefresh();
				mListView.stopLoadMore();
			}
		}, loadTime);
	}

}
